package 数组和链表;

import 抽象数据类型.ListNode;

/**
 * @author: yangwk
 * @date: 2023/4/4
 * @description:
 **/
public class 交换相邻链表 {
    public static ListNode ExchangeAdjacentNodes(ListNode head) {
        if(head == null){
            return null;
        }
        ListNode dummyHead = new ListNode(0);
        dummyHead.next = head;
        ListNode p = dummyHead;
        while(p.next !=null && p.next.next != null){
            ListNode node1 = p.next;
            ListNode node2 = node1.next;
            ListNode next = node2.next;

            node2.next = node1;
            node1.next = next;
            p.next = node2;

            p = node1;
        }
        return dummyHead.next;
    }
}
